//
//  RACSignalViewController.m
//  RACExample
//
//  Created by 山刘 on 2020/6/8.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "RACSignalViewController.h"

@interface RACSignalViewController ()

@end

@implementation RACSignalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSLog(@"sendNext1:%@",[NSThread currentThread]);// sendNext1:<NSThread: 0x600001f36300>{number = 4, name = (null)}
            [subscriber sendNext:@"1"];
            return nil;
        }] subscribeNext:^(id  _Nullable x) {
            NSLog(@"subscribeNext%@:%@",x,[NSThread currentThread]);//subscribeNext1:<NSThread: 0x600001f36300>{number = 4, name = (null)}
        }];
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSLog(@"sendNext1:%@",[NSThread currentThread]);// sendNext1:<NSThread: 0x600003e40040>{number = 4, name = (null)}
            [subscriber sendNext:@"1"];
            return nil;
        }] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id  _Nullable x) {
            NSLog(@"subscribeNext%@:%@",x,[NSThread currentThread]);//subscribeNext1:<NSThread: 0x600003e04980>{number = 1, name = main}
        }];
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSLog(@"sendNext1:%@",[NSThread currentThread]);// sendNext1:<NSThread: 0x6000035fcbc0>{number = 1, name = main}
            [subscriber sendNext:@"1"];
            return nil;
        }] subscribeOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id  _Nullable x) {
            NSLog(@"subscribeNext%@:%@",x,[NSThread currentThread]);//subscribeNext1:<NSThread: 0x6000035fcbc0>{number = 1, name = main}
        }];
    });
}

-(void)test3{
    [[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        NSLog(@"11");[subscriber sendNext:@"发送消息11"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"21");[subscriber sendNext:@"发送消息21"];
            NSLog(@"22");[subscriber sendNext:@"发送消息22"];
        });
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"31");[subscriber sendNext:@"发送消息31"];
            NSLog(@"32");[subscriber sendNext:@"发送消息32"];
            NSLog(@"33");[subscriber sendNext:@"发送消息33"];
        });
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"41");[subscriber sendNext:@"发送消息41"];
            NSLog(@"42");[subscriber sendNext:@"发送消息42"];
            NSLog(@"43");[subscriber sendNext:@"发送消息43"];
            NSLog(@"44");[subscriber sendNext:@"发送消息44"];
        });
        return nil;
    }] throttle:2] subscribeNext:^(id  _Nullable x) {
        NSLog(@"Next:%@",x);
    }];
}

-(void)test1{
    //创建一个普通信号
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        for (NSInteger i = 0; i <= 5; i ++) {
            [subscriber sendNext:@(i)];
        }
        [subscriber sendCompleted];
        return nil;
    }];
    
    //创建一个发送信号的信号，信号的信号
    RACSignal *signalOfSignals = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:signal];
        [subscriber sendCompleted];
        return nil;
    }];
    
    [signalOfSignals subscribeNext:^(id  _Nullable x) {
        //不使用flattenMap，会打印出内部信号
        NSLog(@"订阅signalOfSignals：%@",x);
    }];
    
    [[signalOfSignals flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
        return [value filter:^BOOL(NSNumber*  _Nullable value) {
            NSLog(@"filter：%@",value);
            return [value integerValue] >= 2 ? YES: NO;
        }];
    }] subscribeNext:^(id  _Nullable x) {
        //使用flattenMap，会打印内部信号的值
        NSLog(@"使用flattenMap后订阅signalOfSignals：%@",x);
    }];
}

-(void)test2{
    [[[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        NSLog(@"signalOne");
        [subscriber sendNext:@"signalOne"];
        [subscriber sendCompleted];
        return nil;
    }] then:^RACSignal * _Nonnull{
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSLog(@"signalTwo");
            [subscriber sendNext:@"signalTwo"];
            [subscriber sendCompleted];
            return nil;
        }];
    }] then:^RACSignal * _Nonnull{
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSLog(@"signalThree");
            [subscriber sendNext:@"signalThree"];
            [subscriber sendCompleted];
            return nil;
        }];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"信号被激活:%@",x);
    }];
}
@end
